package blazingbubble.network.lobby.strategy;

import moltencore.network.protocol.LoginResponse;
import spark.network.protocol.Response;
import spark.network.strategy.IProtocolHandleStrategy;
import spark.network.protocol.Base;
import org.apache.log4j.Logger;
import spark.utility.OutWrapper;

import java.util.Collection;

public class LoginResponseStrategy implements IProtocolHandleStrategy<LoginResponse> {
	private Logger logger = Logger.getLogger(LoginResponseStrategy.class);

	private ILoginResponseResultListener loginResponseResultListener;

	public void setLoginResponseResultListener(ILoginResponseResultListener loginResponseResultListener) {
		this.loginResponseResultListener = loginResponseResultListener;
	}

	@Override
	public void handle(LoginResponse packet, OutWrapper<Response> acknowledgement, Collection<Base> additionalData) {
		logger.debug("LoginResponse: accepted " + packet.getAccepted() + "; message " + new String(packet.getMessage()));
		if(loginResponseResultListener != null) {
			if(packet.getAccepted()) {
				loginResponseResultListener.loginSuccessfull();
			}
			else {
				loginResponseResultListener.loginFailed(new String(packet.getMessage()));
			}
		}
	}
}